package com.cake.liulanxiangzhu.security;

import com.cake.liulanxiangzhu.mapper.UserShopMapper;
import com.cake.liulanxiangzhu.pojo.entity.UserRole;
import com.cake.liulanxiangzhu.pojo.entity.UserShop;
import com.cake.liulanxiangzhu.pojo.vo.UserShopLoginVO;
import com.cake.liulanxiangzhu.pojo.vo.UserShopVO;
import com.cake.liulanxiangzhu.utils.LoginUtil;
import com.cake.liulanxiangzhu.web.ProductCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class WeiXinDetailsServiceImpl implements AuthenticationProvider {
    @Autowired
    LoginUtil loginUtil;

    @Autowired
    UserShopMapper userShopMapper;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        log.debug("执行了微信登录验证");
        String code = authentication.getName();
        String openId = loginUtil.getOpenId(code);
        log.debug("微信用户的openId：{}",openId);
        UserShopLoginVO userShopLoginVO = userShopMapper.loginByUserId(openId);
        if (userShopLoginVO!=null){
            List<GrantedAuthority> authorities = new ArrayList<>();
            for (String permission : userShopLoginVO.getPermissions()) {
                GrantedAuthority authority = new SimpleGrantedAuthority(permission);
                authorities.add(authority);
            }
            AdminDetails adminDetails = new AdminDetails(String.valueOf(userShopLoginVO.getUserId()) ,openId,true,authorities);
            adminDetails.setId(userShopLoginVO.getUserId());
            return new UsernamePasswordAuthenticationToken(adminDetails,code,adminDetails.getAuthorities());
        }else {
            UserShop user = new UserShop();
            user.setOpenId(openId);
            user.setIsRegister(ProductCode.NO.getCode());
            user.setIsVip(ProductCode.NO.getCode());
            user.setVipLevel(ProductCode.NO.getCode());
            user.setCreatTime(new Date());
            user.setUpdateTime(new Date());
            userShopMapper.insert(user);
            UserShopVO userShopVO = userShopMapper.queryUserId(openId);
            UserRole userRole = new UserRole();
            userRole.setUserId(userShopVO.getUserId());
            userRole.setRoleId(3);
            userRole.setCreatTime(LocalDateTime.now());
            userRole.setUpdateTime(LocalDateTime.now());
            userShopMapper.insertUserRole(userRole);
            UserShopLoginVO loginVO = userShopMapper.loginByUserId(openId);
            List<GrantedAuthority> authorities = new ArrayList<>();
            for (String permission : loginVO.getPermissions()) {
                GrantedAuthority authority = new SimpleGrantedAuthority(permission);
                authorities.add(authority);
            }
            AdminDetails adminDetails = new AdminDetails(loginVO.getOpenId(),null,true,authorities);
            adminDetails.setId(userShopVO.getUserId());
            return new UsernamePasswordAuthenticationToken(adminDetails,null,adminDetails.getAuthorities());
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }
}
